﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;


namespace Marmat.Forms
{
    [Designer("Marmat.Forms.Design.DateTimePickerIntervalDesigner")]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(System.Windows.Forms.DateTimePicker))]
    public partial class DateTimePickerInterval
    {
        public DateTimePickerInterval()
        {
            InitializeComponent();
            DateTimePicker1.Name = "DateTimePicker1";
            DateTimePicker2.Name = "DateTimePicker2";
        }

        [DefaultValue(typeof(DateTimeFormatEnum), "Date")]
        public DateTimeFormatEnum DateTimeFormat
        {
            get
            {
                return this.m_DateTimeFormat;
            }

            set
            {
                this.m_DateTimeFormat = value;
                this.DateTimePicker1.DateTimeFormat = value;
                this.DateTimePicker2.DateTimeFormat = value;
            }
        }

        private DateTimeFormatEnum m_DateTimeFormat = DateTimeFormatEnum.Date;
        /// <summary>
        /// 设置或返回控件标题的文本。
        /// </summary>
        /// <returns></returns>
        [DefaultValue(null)]
        [Category("标题")]
        [Description("设置或返回控件标题的文本。")]
        public string Caption
        {
            get
            {
                return this.DateTimePicker1.Caption;
            }

            set
            {
                this.DateTimePicker1.Caption = value;
            }
        }

        /// <summary>
        /// 获取或设置是否显示标题
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(true)]
        [Category("标题")]
        [Description("设置或返回是否显示控件标题的文本。")]
        public bool ShowCaption
        {
            get
            {
                return this.DateTimePicker1.ShowCaption;
            }

            set
            {
                this.DateTimePicker1.ShowCaption = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 获取或设置控件显示的标题的显示位置。
        /// </summary>
        /// <returns></returns>
        [Category("标题")]
        [Description("获取或设置控件显示的标题的显示位置。")]
        public ContentAlignment CaptionTextAlign
        {
            get
            {
                return this.DateTimePicker1.CaptionTextAlign;
            }

            set
            {
                this.DateTimePicker1.CaptionTextAlign = value;
            }
        }

        /// <summary>
        /// 设置或返回标题文本的宽度
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(75)]
        [Category("标题")]
        [Description("设置或返回标题文本的宽度。")]
        public int CaptionWidth
        {
            get
            {
                return this.DateTimePicker1.CaptionWidth;
            }

            set
            {
                this.DateTimePicker1.CaptionWidth = value;
            }
        }

        [DefaultValue(typeof(Font), "微软雅黑,9.0pt")]
        [Category("标题")]
        [Description("获取或设置控件显示的标题的字体")]
        public Font CaptionFont
        {
            get
            {
                return this.DateTimePicker1.CaptionFont;
            }

            set
            {
                this.DateTimePicker1.CaptionFont = value;
            }
        }

        [Category("标题")]
        [Description("获取或设置控件显示的标题的字体颜色")]
        public Color CaptionForeColor
        {
            get
            {
                return this.DateTimePicker1.CaptionForeColor;
            }

            set
            {
                this.DateTimePicker1.CaptionForeColor = value;
            }
        }

        [DefaultValue(typeof(Color), "Transparent")]
        [Category("标题")]
        [Description("获取或设置控件显示的标题的背景颜色")]
        public Color CaptionBackColor
        {
            get
            {
                return this.DateTimePicker1.CaptionBackColor;
            }

            set
            {
                this.DateTimePicker1.CaptionBackColor = value;
            }
        }

        /// <summary>
        /// 开始日期
        /// </summary>
        /// <returns></returns>
        public DateTime StartDate
        {
            get
            {
                return this.DateTimePicker1.Value;
            }

            set
            {
                this.DateTimePicker1.Value = value;
            }
        }

        /// <summary>
        /// 结束日期
        /// </summary>
        /// <returns></returns>
        public DateTime EndDate
        {
            get => this.DateTimePicker2.Value;
            set => this.DateTimePicker2.Value = value;
        }

        /// <summary>
        /// 将 StartDate 属性重置为其默认值。
        /// </summary>
        protected internal void ResetStartDate()
        {
            this.StartDate = default;
        }

        /// <summary>
        /// 是否应该序列化 StartDate 属性。
        /// </summary>
        /// <returns></returns>
        protected internal bool ShouldSerializeStartDate()
        {
            var st = new DateTime(0);
            return !(this.StartDate == default || this.StartDate == st);
        }

        /// <summary>
        /// 将 EndDate 属性重置为其默认值。
        /// </summary>
        protected internal void ResetEndDate()
        {
            this.EndDate = default;
        }

        /// <summary>
        /// 是否应该序列化 EndDate 属性。
        /// </summary>
        /// <returns></returns>
        protected internal bool ShouldSerializeEndDate()
        {
            var st = new DateTime(0);
            return !(this.EndDate == default || this.EndDate == st);
        }

        /// <summary>
        /// 将 CaptionTextAlign 属性重置为其默认值。
        /// </summary>
        protected internal void ResetCaptionTextAlign()
        {
            this.CaptionTextAlign = default;
        }

        /// <summary>
        /// 是否应该序列化 CaptionTextAlign 属性。
        /// </summary>
        /// <returns></returns>
        protected internal bool ShouldSerializeCaptionTextAlign()
        {
            return this.DateTimePicker1._CaptionTextAlign != default;
        }

        /// <summary>
        /// 将 CaptionForeColor 属性重置为其默认值。
        /// </summary>
        protected internal void ResetCaptionForeColor()
        {
            this.CaptionForeColor = default;
        }

        /// <summary>
        /// 是否应该序列化 CaptionForeColor 属性。
        /// </summary>
        /// <returns></returns>
        protected internal bool ShouldSerializeCaptionForeColor()
        {
            return this.DateTimePicker1._CaptionForeColor != default;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            int left = this.DateTimePicker1.Left + (this.ShowCaption ? this.DateTimePicker1.CaptionWidth : 0);
            var rect = new Rectangle(left + 1, 0, this.Width - left, this.Height);
            var color = this.DateTimePicker1.m_BaseEdit.BackColor;
            using (var brush = new SolidBrush(color))
            {
                e.Graphics.FillRectangle(brush, rect);
            }

            int a = (int)Math.Round(Math.Min(255d, color.A * 1.3d));
            int r = (int)Math.Round(Math.Max(0d, color.R / 1.3d));
            int g = (int)Math.Round(Math.Max(0d, color.G / 1.3d));
            int b = (int)Math.Round(Math.Max(0d, color.B / 1.3d));
            using (var pen = new Pen(Color.FromArgb(a, r, g, b)))
            {
                e.Graphics.DrawRectangle(pen, new Rectangle(left - 1, 0, this.Width - left, this.Height - 1));
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (this.DateTimePicker1.m_BaseEdit is object)
            {
                int height = this.DateTimePicker1.m_BaseEdit.Height + 2;
                if (this.Height != height)
                {
                    this.Height = height;
                }
            }
        }

        private void DateTimePicker1_SizeChanged(object sender, EventArgs e)
        {
            if (this.DateTimePicker1.m_BaseEdit is object)
            {
                int height = this.DateTimePicker1.m_BaseEdit.Height + 2;
                if (this.Height != height)
                {
                    this.Height = height;
                }
            }
        }

        private void DateTimePicker1_Paint(object sender, PaintEventArgs e)
        {
            var color = this.DateTimePicker1.m_BaseEdit.BackColor;
            int a = (int)Math.Round(Math.Min(255d, color.A * 1.3d));
            int r = (int)Math.Round(Math.Max(0d, color.R / 1.3d));
            int g = (int)Math.Round(Math.Max(0d, color.G / 1.3d));
            int b = (int)Math.Round(Math.Max(0d, color.B / 1.3d));
            using (var pen = new Pen(Color.FromArgb(a, r, g, b)))
            {
                int left = this.DateTimePicker1.CaptionWidth - 1;
                e.Graphics.DrawLine(pen, left, 0, left, this.DateTimePicker1.Height);
            }
        }

        private void DateTimePickerInterval_SizeChanged(object sender, EventArgs e)
        {
            this.RefreshLayout();
        }

        /// <summary>
        /// 刷新布局
        /// </summary>
        private void RefreshLayout()
        {
            int startWidth = this.ShowCaption ? this.DateTimePicker1.CaptionWidth : 0;
            int endWidth = this.DateTimePicker2.CaptionWidth;
            int width = (int)Math.Round((this.Width - 2 - startWidth - endWidth) / 2d);
            this.DateTimePicker1.Left = 1;
            this.DateTimePicker1.Width = startWidth + width;
            this.DateTimePicker2.Left = this.DateTimePicker1.Right + 1;
            this.DateTimePicker2.Width = endWidth + width;
        }

        private void DateTimePicker1_Enter(object sender, EventArgs e)
        {
            SendKeys.Send("{F4}");
        }
    }
}